import {
  upkeepList,
  upkeepDetail,
  upkeepAdd,
  upkeepEdit,
  upkeepInfoAdd,
  upkeepInfoEdit,
  upkeepInfoList,
  upkeepInfoDetail,
  importUpkeepDetail,
  generatedBills,
  deleteUpkeep,
  deleteUpkeepInfo,
  selectAll,
} from '@/services/upkeep';
import prompt from '@/components/Prompt';

export default {
  namespace: 'upkeep',

  state: {
    upkeepList: {},
    detail: {},
    upkeepInfoList: {},
    infoDetail: {},
  },

  effects: {
    *selectAll({ payload, callback }, { call }) {
      const response = yield call(selectAll, payload);
      if (response.code === 0) {
        prompt({ content: `账单生成${response.message}!` });
        if (callback) callback(response.data);
      } else {
        if (callback) callback();
      }
    },
    *upkeepList({ payload, callback }, { call, put }) {
      const response = yield call(upkeepList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        callback && callback(list);
        yield put({
          type: 'list',
          payload: {
            upkeepList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
      }
    },

    *upkeepDetail({ payload, callback }, { call, put }) {
      const response = yield call(upkeepDetail, payload);
      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            detail: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *upkeepInfoDetail({ payload }, { call, put }) {
      const response = yield call(upkeepInfoDetail, payload);
      if (response.code === 0) {
        yield put({
          type: 'infoDetailPut',
          payload: {
            infoDetail: response.data,
          },
        });
      }
    },
    *upkeepInfoList({ payload }, { call, put }) {
      const response = yield call(upkeepInfoList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'info',
          payload: {
            upkeepInfoList: {
              [payload.id]: {
                pagination: {
                  current: pageNum,
                  pageSize,
                  total,
                },
                list,
              },
            },
          },
        });
      }
    },
    *upkeepInfoOperate({ payload, callback }, { call }) {
      const response = yield call(payload.id ? upkeepInfoEdit : upkeepInfoAdd, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
    *upkeepOperate({ payload, callback }, { call }) {
      const response = yield call(payload.id ? upkeepEdit : upkeepAdd, payload);

      if (response.code === 0) {
        if (payload.id) {
          prompt({ content: `编辑${response.message}` });
        }

        if (callback) callback(response.data);
      }
    },

    *importUpkeepDetail({ payload, callback }, { call }) {
      const response = yield call(importUpkeepDetail, payload);
      if (response.code === 0) {
        prompt({ content: response.message });
      }
    },
    /**
     * 生成账单
     */
    *generatedBills({ payload, callback }, { call }) {
      const response = yield call(generatedBills, payload);
      const { code, message, data } = response;
      if (code === 0) {
        prompt({ content: `账单生成${message}!` });
        if (callback) callback(data);
      }
    },
    *deleteUpkeep({ payload, callback }, { call }) {
      const response = yield call(deleteUpkeep, payload);
      const { code, message } = response;
      if (code === 0) {
        prompt({ content: `删除${message}` });
        if (callback) callback();
      }
    },
    *deleteUpkeepInfo({ payload, callback }, { call }) {
      const response = yield call(deleteUpkeepInfo, payload);
      const { code, message } = response;
      if (code === 0) {
        prompt({ content: `删除${message}` });
        if (callback) callback();
      }
    },
  },

  reducers: {
    infoDetailPut(state, { payload }) {
      return {
        ...state,
        infoDetail: payload.infoDetail,
      };
    },
    cars(state, { payload }) {
      return {
        ...state,
        carList: payload.carList,
      };
    },
    list(state, { payload }) {
      return {
        ...state,
        upkeepList: payload.upkeepList,
      };
    },
    info(state, { payload }) {
      return {
        ...state,
        upkeepInfoList: { ...state.upkeepInfoList, ...payload.upkeepInfoList },
      };
    },
    detail(state, { payload }) {
      return {
        ...state,
        detail: { ...state.detail, ...payload.detail },
      };
    },
  },
};
